import axios from 'axios';

/*
* LOGIN
*/
export const LoginUser = (email: string, password: string) => {
  axios({
    method: 'POST',
    url: 'http://localhost:8080/user/login',
    data: {
      "email": email,
      "password": password
    }
  })
  .then((res) => {
    if (res.data.success) {
      // store satoken key:value pair in local storage
      const key = res.data['data']['tokenName'];
      const value = res.data['data']['tokenValue'];
      window.localStorage.setItem(key, value);

      // store User Role in local storage
      const userRole = res.data['msg'];
      window.localStorage.setItem('userRole', userRole);

      // store User Id in local storage
      const userId = res.data['data']['loginId'];
      window.localStorage.setItem('userId', userId);

      // store User Email in local storage
      window.localStorage.setItem('userEmail', email);

      // redirect to respective homepage according to User Role
      if (userRole === 'stu') {window.location.href = '/student/home';}
      else if (userRole === 'staff') {window.location.href = '/staff/home';}
      else if (userRole === 'admin' || userRole === 'super-admin') {window.location.href = '/admin/home';}
    }
    else {
      // wrong password / account not registered
      alert(res.data.msg);
    }
  });
};

/*
* LOGOUT
*/
export const LogoutUser = () => {
  axios({
    method: 'GET',
    headers: { "satoken": window.localStorage.getItem("satoken") },
    url: 'http://localhost:8080/user/logout',
  })
  .then(() => {
    window.location.href = '/login';
    window.localStorage.setItem("satoken", "null");
  })
}

/*
* REGISTER
*/
export const RegisterUser = (name: string, email: string, password: string) => {
  axios({
    method: 'POST',
    url: 'http://localhost:8080/user/register',
    data: {
      "name": name,
      "email": email,
      "password": password
    }
  })
  .then((res) => {
    if (res.data.success) {
      console.log("Registered successfully!");
      LoginUser(email, password);
    }
    else {
      // email has already been registered
      alert(res.data.msg);
    }
  })
}

/*
* ADD SUPERVISION
*/
export const AddSupervision = (moduleId: number) => {
  axios({
    method: 'POST',
    headers: { "satoken": window.localStorage.getItem("satoken") },
    url: 'http://localhost:8080/supervision/save',
    data: {
      "moduleId": moduleId,
      "email": window.localStorage.getItem("userEmail"),
      "taBool": 0,
    }
  })
  .then(() => {
    window.location.href = '/staff/module'
  })
}

/*
* ADD MODULE
*/
export const AddModule = (code: string, sem: string, name: string) => {
  axios({
    method: 'POST',
    headers: { "satoken": window.localStorage.getItem("satoken") },
    url: 'http://localhost:8080/module/save',
    data: {
      "code": code,
      "semester": sem,
      "name": name
    }
  })
  .then((res) => {
    if (res.data.success) {
      // Assign staff_id to this newly added module
      AddSupervision(res.data["data"])
    }
    else {
      alert("Module already exists!")
    }
  })
}

/*
* ADD ENROLLMENT
*/
export const AddStudentEnrollment = (moduleId: number, email: string) => {
  axios({
    method: 'POST',
    headers: { "satoken": window.localStorage.getItem("satoken") },
    url: 'http://localhost:8080/enrollment/save',
    data: {
      "moduleId": moduleId,
      "email": email,
    }
  })
  .then((res) => {
    if (res.data.success) {
      window.location.href = `/staff/module/${moduleId}/students`
    }
    else {
      alert(res.data.msg);
    }
  })
}

/*
* REMOVE ENROLLMENT
*/
export const RemoveStudentEnrollment = (moduleId: number, email: string) => {
  axios({
    method: 'POST',
    headers: { "satoken": window.localStorage.getItem("satoken") },
    url: 'http://localhost:8080/enrollment/delete',
    data: {
      "moduleId": moduleId,
      "email": email,
    }
  })
  .then((res) => {
    if (res.data.success) {
      window.location.href = `/staff/module/${moduleId}/students`
    }
    else {
      alert(res.data.msg);
    }
  })
}

/*
* ADD TA APPOINTMENT
*/
export const AddTAappointment = (moduleId: number, email: string, taBool: number) => {
  axios({
    method: 'POST',
    headers: { "satoken": window.localStorage.getItem("satoken") },
    url: 'http://localhost:8080/supervision/save',
    data: {
      "moduleId": moduleId,
      "email": email,
      "taBool": taBool,
    }
  })
  .then((res) => {
    if (res.data.success) {
      window.location.href = `/staff/module/${moduleId}/TAs`
    }
    else {
      alert(res.data.msg);
    }
  })
}

/*
* REMOVE TA APPOINTMENT
*/
export const RemoveTAappointment = (moduleId: number, email: string) => {
  axios({
    method: 'POST',
    headers: { "satoken": window.localStorage.getItem("satoken") },
    url: 'http://localhost:8080/supervision/delete',
    data: {
      "moduleId": moduleId,
      "email": email,
    }
  })
  .then((res) => {
    if (res.data.success) {
      window.location.href = `/staff/module/${moduleId}/TAs`
    }
    else {
      alert(res.data.msg);
    }
  })
}
